#ifndef LISTMODEL_H
#define LISTMODEL_H

#include "QAbstractListModel"
#include "listitem.h"
#include "baza/elementpopisa.h"
#include "QDebug"

class ListModel : public QAbstractListModel
{
    Q_OBJECT

    Q_PROPERTY(int count READ count NOTIFY countChanged)

public:
    enum ListItemRoles  {
        RoleItemName = Qt::DisplayRole,
        RoleListItemId = Qt::UserRole,
        RoleQuantity = Qt::UserRole+1,
        RoleBought = Qt::UserRole+2,
        RoleUnit = Qt::UserRole+3,
        RoleItemId = Qt::UserRole+4,
        RoleGroupId = Qt::UserRole+5,
        RoleGroupName = Qt::UserRole+6,
        RoleIsAGroup = Qt::UserRole+7,
        RoleGroupNumber = Qt::UserRole+8,
        RoleIndex = Qt::UserRole+9
    };

    ListModel(QObject *parent = 0);

    void addListItem(const ElementPopisa &elementPopisa, bool isAGroup, int groupNumber, int index);
    void clear();

    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    int count();

    Q_INVOKABLE ListItem* get(int index);
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

    Qt::ItemFlags flags(const QModelIndex &index) const;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role) const;

    QModelIndex indexFromItem(const ListItem *item) const;

private slots:
    void handleItemChange();

signals:
    void countChanged(int newCount);

private:
    QList<ListItem*> m_listItems;
};

#endif // LISTMODEL_H
